package ink.lovejinhu.leetcode;

/**
 * @author jinhu
 * created 2021-08-03 19:39
 */
public class Solution_0724 {
    public int pivotIndex(int[] nums) {
        int leftSum = 0;
        int rightSum = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i == 0) {
                leftSum = 0;
                rightSum = this.computeSum(nums, i + 1, nums.length - 1);

            } else if (i == nums.length - 1) {
                leftSum = this.computeSum(nums, 0, nums.length - 2);
                rightSum = 0;
            } else {
                leftSum = this.computeSum(nums, 0, i - 1);
                rightSum = this.computeSum(nums, i + 1, nums.length - 1);
            }
            if (leftSum == rightSum) {
                return i;
            }
        }
        return -1;
    }

    public int computeSum(int[] nums, int left, int right) {
        int sum = 0;
        for (int i = left; i <= right; i++) {
            sum += nums[i];
        }
        return sum;
    }
}
